package com.wenyl.juc.thread.exchangeTest;

import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Swimming Dragon
 * @description: TODO
 * @date 2024年11月07日 11:22
 */
public class Test {
    private static final Exchanger<String> ex = new Exchanger<>();
    private static ExecutorService pool = Executors.newFixedThreadPool(2);
    public static void main(String[] args) {
        pool.execute(() -> {
            String A = "银行流水A";
            String B = null;
            try {
                B = ex.exchange(A);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("threadA:"+B);
        });

        pool.execute(() -> {
            String B = "银行流水B";
            String A = null;
            try {
                A = ex.exchange(B);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("threadB:"+A);
        });
    }
}
